﻿package mortal.game.net.command.group
{
    import Framework.MQ.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.net.broadCast.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.mvc.core.*;

    public class GroupInfoCommand extends BroadCastCall
    {

        public function GroupInfoCommand(param1:Object)
        {
            super(param1);
            return;
        }// end function

        override public function call(param1:MessageBlock) : void
        {
            var msg:SGroup;
            var oldCopyCode:int;
            var player:SGroupPlayer;
            var oldPlayer:SGroupPlayer;
            var tcopy:TCopy;
            var isTeamInCopy:Boolean;
            var mapArr:Array;
            var k:SGroupPlayer;
            var mb:* = param1;
            msg = mb.messageBase as SGroup;
            var oldSgroup:* = _cache.group.sgroup;
            var oldCaptainId:* = _cache.group.captain;
            var oldGroupData:* = _cache.group.players;
            var newGroupData:* = msg.players;
            if (oldSgroup)
            {
                oldCopyCode = oldSgroup.copyCode;
            }
            _cache.group.sgroup = msg;
            _cache.group.captain = msg.captainId;
            _cache.group.groupId = msg.groupId;
            _cache.group.players = msg.players;
            _cache.group.groupName = msg.name;
            _cache.group.memberInvite = msg.memberInvite;
            _cache.group.autoEnter = msg.autoEnter;
            if (oldSgroup && msg.copyCode && oldSgroup.copyCode != msg.copyCode && !_cache.group.isCaptain && msg.copyCode != -1)
            {
                var closeAlert:* = function (param1:int) : void
            {
                var _loc_2:GroupCopyLeaveOperData = null;
                if (param1 == Alert.CANCEL)
                {
                    _loc_2 = new GroupCopyLeaveOperData();
                    _loc_2.isShowAler = false;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave, _loc_2));
                }
                return;
            }// end function
            ;
                tcopy = CopyConfig.instance.getCopyInfoByCode(msg.copyCode);
                Alert.alertWinRenderer = GroupCopyLeaveWin;
                Alert.calcelLabel = Language.getString(20504);
                Alert.show(Language.getStringByParam(30806, tcopy.name), null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            }
            if (Cache.instance.group.isInCopyGroup)
            {
                if (GroupCopyPasswordWin.isViweShow && (oldSgroup && msg.copyCode && oldSgroup.copyCode != msg.copyCode || oldSgroup == null))
                {
                    GroupCopyPasswordWin.instance.hide();
                }
                if (_cache.group.isCaptain && msg.players.length == 5 && (newGroupData.length > oldGroupData.length || msg.copyCode != oldCopyCode))
                {
                    mapArr = CopyConfig.instance.getCopyInfoByCode(msg.copyCode).maps.split("#");
                    var _loc_3:int = 0;
                    var _loc_4:* = msg.players;
                    while (_loc_4 in _loc_3)
                    {
                        
                        k = _loc_4[_loc_3];
                        if (mapArr.indexOf(k.player.mapId.toString()) >= 0)
                        {
                            isTeamInCopy;
                            break;
                        }
                    }
                    if (!isTeamInCopy)
                    {
                        if (!CopyUtil.isInCopy)
                        {
                            var autoEnter:* = function () : void
            {
                if (CopyUtil.isInCopy)
                {
                }
                else
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupEnter, msg.copyCode));
                }
                return;
            }// end function
            ;
                            if (Cache.instance.group.groupCopyAutoEnter)
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupEnter, msg.copyCode));
                            }
                        }
                        else if (CopyUtil.isInCopy && Cache.instance.copy.copyInfo.type != ECopyType._ECopyTypeTeam)
                        {
                            Alert.calcelLabel = Language.getString(20505);
                            Alert.show(Language.getStringByParam(20506, CopyConfig.instance.getCopyInfoByCode(msg.copyCode).name), null, Alert.CANCEL, null, null, null, 4);
                        }
                    }
                }
            }
            if (oldGroupData.length == 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(30241));
                if (_cache.group.inviteList.length)
                {
                    _cache.group.inviteList = [];
                    NetDispatcher.dispatchCmd(ServerCommand.GroupInvite, null);
                }
            }
            else if (!EntityUtil.isSelf(oldCaptainId) && EntityUtil.isSelf(msg.captainId))
            {
                MsgManager.showRollTipsMsg(Language.getString(30247));
            }
            else if (EntityUtil.isSelf(oldCaptainId) && !EntityUtil.isSelf(msg.captainId))
            {
                _cache.group.applyList = [];
                NetDispatcher.dispatchCmd(ServerCommand.GroupApply, null);
            }
            if (oldCaptainId == null || !EntityUtil.equal(oldCaptainId, msg.captainId))
            {
                NetDispatcher.dispatchCmd(ServerCommand.CaptainChange, null);
            }
            var i:int;
            var j:int;
            var exist:Boolean;
            if (newGroupData.length > oldGroupData.length)
            {
                i;
                while (i < newGroupData.length)
                {
                    
                    player = newGroupData[i] as SGroupPlayer;
                    exist;
                    j;
                    while (j < oldGroupData.length)
                    {
                        
                        oldPlayer = oldGroupData[j] as SGroupPlayer;
                        if (EntityUtil.equal(player.player.entityId, oldPlayer.player.entityId))
                        {
                            exist;
                        }
                        j = (j + 1);
                    }
                    if (!exist)
                    {
                        if (!EntityUtil.isSelf(player.player.entityId))
                        {
                            MsgManager.showRollTipsMsg(Language.getStringByParam(30242, player.player.name));
                            if (_cache.group.isCaptain && newGroupData.length == 5)
                            {
                                NetDispatcher.dispatchCmd(ServerCommand.GetNearPlayer, null);
                            }
                        }
                    }
                    i = (i + 1);
                }
            }
            else if (newGroupData.length < oldGroupData.length)
            {
                i;
                while (i < oldGroupData.length)
                {
                    
                    player = oldGroupData[i] as SGroupPlayer;
                    exist;
                    j;
                    while (j < newGroupData.length)
                    {
                        
                        oldPlayer = newGroupData[j] as SGroupPlayer;
                        if (EntityUtil.equal(player.player.entityId, oldPlayer.player.entityId))
                        {
                            exist;
                        }
                        j = (j + 1);
                    }
                    if (!exist)
                    {
                        if (!EntityUtil.isSelf(player.player.entityId))
                        {
                            MsgManager.showRollTipsMsg(Language.getStringByParam(30245, player.player.name));
                            NetDispatcher.dispatchCmd(ServerCommand.TeamMateLeave, player.player.entityId);
                            if (_cache.group.isCaptain)
                            {
                                NetDispatcher.dispatchCmd(ServerCommand.GetNearPlayer, null);
                            }
                        }
                    }
                    i = (i + 1);
                }
            }
            NetDispatcher.dispatchCmd(ServerCommand.GroupPlayerInfoChange, _cache.group.players);
            return;
        }// end function

    }
}
